package org.example.notificationservice.controller;

import lombok.RequiredArgsConstructor;
import org.example.notificationservice.entity.Notification;
import org.example.notificationservice.service.NotificationService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Map;

@RestController
@RequestMapping("/api/notifications")
@RequiredArgsConstructor
public class NotificationController {
    private final NotificationService notificationService;

    @PostMapping
    public ResponseEntity<Notification> createNotification(
            @RequestHeader("X-User-ID") String userId,
            @RequestParam String templateId,
            @RequestBody Map<String, Object> params) {
        return ResponseEntity.ok(notificationService.createNotification(userId, templateId, params));
    }

    @PostMapping("/{id}/send")
    public ResponseEntity<Void> sendNotification(@PathVariable String id) {
        Notification notification = notificationService.getNotificationById(id);
        notificationService.sendNotification(notification);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/{id}/schedule")
    public ResponseEntity<Void> scheduleNotification(
            @PathVariable String id,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime scheduledTime) {
        Notification notification = notificationService.getNotificationById(id);
        notificationService.scheduleNotification(notification, scheduledTime);
        return ResponseEntity.ok().build();
    }
}